var JsOper=require("../Tool/JsOper.js");
var DataTable=require("../Tool/CDataTableName.js");
var GuestModel={
	//当前住客信息
	GetTodayGuests:function(MySql,aCode,aUTCDate,aResponse) {
		var QueryCmd=['SELECT t1.ROOM_ID,t1.GASTNR,t1.GASTNAME,t2.SEX,t2.VIP,t1.ANREISE,t1.ABREISE,t1.CHECK_FLAG '];
		QueryCmd.push(' FROM  ',DataTable.TableGuestCheckIn,' t1, ',DataTable.TableGuestInfo,' t2 ' );
		QueryCmd.push(' WHERE t1.HOTELCODE=? AND t1.HOTELCODE=t2.HOTELCODE AND t1.GASTNR=t2.NUMMER  AND ((t1.ANREISE<? AND t1.ABREISE>=?) OR  CHECK_FLAG=?) AND CHECK_FLAG<>? ' );
		QueryCmd.push(' ORDER BY t1.ROOM_ID ' );
		var NowDate=JsOper.GetDateStr(aUTCDate,0);
		var NextDate=JsOper.GetDateStr(aUTCDate,1);
		var QueryPara=[aCode,NextDate,NowDate,DataTable.FlagCheckIn,DataTable.FlagCheckOut];
		var runCmd=QueryCmd.join("");
	  	MySql.ExecuteQueryCmd(runCmd,QueryPara
	  		,function(data){
	  			if(data===false){
	  				var DataRst={iRst:-1,iGuestList:[]};
	  				aResponse?aResponse(DataRst):0;
	  			}
	  			var RoomGuest=[];
	  			for(var i in data)
		  		{
	                RoomGuest.push(
	                {
	                	iRoomId:data[i].ROOM_ID,
	                    iGuestNo :data[i].GASTNR,
	                    iGuestName :data[i].GASTNAME,
	                    iSex : data[i].SEX,
	                    iVip : data[i].VIP,
	                    iCheckFlag :data[i].CHECK_FLAG,
	                    iCheckOutTime :Date.parse(data[i].ABREISE)/1000,
	                    iCheckInTime :Date.parse(data[i].ANREISE)/1000
	                });
	                
		  		}
		  		var DataRst={iRst:0,iGuestList:RoomGuest};
				aResponse?aResponse(DataRst):0;
			});
	},
	//房间预订信息
	GetRoomBooking:function(MySql,aCode,aRoomId,aNowTime,aResponse) {
		var QueryCmd=['SELECT t1.GASTNR,t1.GASTNAME,t2.SEX,t2.VIP,t1.ROOM_ID,t1.ANREISE,t1.ABREISE,t1.CHECK_FLAG,t1.RT7,t1.TEAM '];
		QueryCmd.push(' FROM  ',DataTable.TableGuestCheckIn,' t1, ',DataTable.TableGuestInfo,' t2 ' );
		QueryCmd.push(' WHERE t1.HOTELCODE=? AND t1.HOTELCODE=t2.HOTELCODE AND t1.GASTNR=t2.NUMMER  AND ROOM_ID=? AND t1.ABREISE>=? AND CHECK_FLAG=? ' );
		var ThisDate=new Date(aNowTime*1000);
		var NowDate=JsOper.ToLongDateStr(ThisDate);
		var QueryPara=[aCode,aRoomId,NowDate,DataTable.FlagEmpty];
		var runCmd=QueryCmd.join("");
	  	MySql.ExecuteQueryCmd(runCmd,QueryPara
	  		,function(data){
	  			if(data===false){
	  				var DataRst={iRst:-1,iGuestList:[]};
	  				aResponse?aResponse(DataRst):0;
	  			}
	  			var RoomGuest=[];
	  			for(var i in data)
		  		{
	                RoomGuest.push(
	                {
	                    iGuestNo :data[i].GASTNR,
	                    iGuestName :data[i].GASTNAME,
	                    iSex : data[i].SEX,
	                    iVip : data[i].VIP,
	                    iRoomId:data[i].ROOM_ID,
	                    iCheckFlag :data[i].CHECK_FLAG,
	                    iCheckOutTime :Date.parse(data[i].ABREISE)/1000,
	                    iCheckInTime :Date.parse(data[i].ANREISE)/1000,
	                    iCompany:data[i].RT7,
	                    iCSource:data[i].TEAM
	                });
	                
		  		}
		  		var DataRst={iRst:0,iGuestList:RoomGuest};
				aResponse?aResponse(DataRst):0;
			});
	},
	//获取客人信息
	GetGuestInfoById:function(MySql,aUserId,aResponse){
	  	MySql.SelectTable(DataTable.TableGuestInfo,"","NUMMER="+aUserId,false
	  		,function(data){
	  			if(data===false){
	  				var DataRst={iRst:-1,iGuest:[]};
	  				aResponse?aResponse(DataRst):0;
	  			}
	  			var Guest=[];
	  			for(var i in data)
		  		{
	                Guest.push(
	                {
	                    iGuestNo :data[i].GASTNR,
	                    iGuestName :data[i].GASTNAME,
	                    iCity:data[i].ORT,
	                    iEmail:data[i].EMAIL,
	                    iLang:data[i].SPRACHE,
	                    iMobile:data[i].TELEX,
	                    iNation:data[i].NATION,
	                    iSex : data[i].SEX,
	                    iVip : data[i].VIP,
	                    iCardNo:data[i].PASSPORT
	                });
		  		}
		  		var DataRst={iRst:0,iGuest:Guest};
				aResponse?aResponse(DataRst):0;
			});
	},
	//获取团队信息
	GetAllTeamName:function(MySql,aCode,aResponse){
        var Clause=" HOTELCODE=? AND TEAM>'' AND CHECK_FLAG!='E' GROUP BY TEAM "
		var Param=[aCode];
		MySql.SelectTable(DataTable.TableGuestCheckIn," TEAM ",Clause,Param,
			function(data){
				if(data===false){aResponse?aResponse(-1):0;return;}
				var Teams=[];
	  			for(var i in data)
		  		{
	                Teams.push(data[i].TEAM);
		  		}
		  		var DataRst={iRst:0,iTeam:Teams};
				aResponse?aResponse(DataRst):0;
			});
	},
	//获取所有客人信息
	GetAllGuestList:function(MySql,aCode,aResponse){
		var Clause=" HOTELCODE=? "
		var Param=[aCode];
		MySql.SelectTable(DataTable.TableGuestInfo," DISTINCT NUMMER,NAME,SEX,VIP ",Clause,Param,
			function(data){
				if(data===false){aResponse?aResponse(-1):0;return;}
				var RoomGuest=[];
	  			for(var i in data)
		  		{
	                RoomGuest.push(
	                {
	                    iGuestNo :data[i].NUMMER,
	                    iGuestName :data[i].NAME,
	                    iSex : data[i].SEX,
	                    iVip : data[i].VIP
	                   
	                });
		  		}
		  		var DataRst={iRst:0,iGuest:RoomGuest};
				aResponse?aResponse(DataRst):0;
			});
	},
	GetTeamNewGuestName:function(MySql,aCode,aTeam,aIndex,aResponse){
		var Clause=" HOTELCODE=? AND TEAM=? AND TEAM>'' "
		var Param=[aCode,aTeam];
		MySql.SelectTable(DataTable.TableGuestCheckIn," COUNT(1) as num ",Clause,Param,
			function(data){
				if(data===false){aResponse?aResponse(-1):0;return;}
				var RoomGuest=[];
				var TeaName=aTeam;
				if(data.length>0)
				{
					TeaName=aTeam+(parseInt(aIndex)+1+parseInt(data[0].num));
				}else{
					TeaName=aTeam+(parseInt(aIndex)+1);
				}
		  		var DataRst={iRst:0,iName:TeaName};
				aResponse?aResponse(DataRst):0;
			});
	},
	//新增客人信息
	NewGuestInfo:function(MySql,aCode,aGuestInfo,aResponse){
		var QPara=[aGuestInfo.iGuestName,aGuestInfo.iCardNo,aGuestInfo.iGuestNo];
		MySql.SelectTable(DataTable.TableGuestInfo,"NUMMER","NAME=? AND PASSPORT=? AND NUMMER<>? ",QPara
			,function(data){
				if(data===false){var DataRst={iRst:-1,iNewId:0};aResponse?aResponse(DataRst):0;return;}
				if(data.length>0&&aGuestInfo.iGuestNo>0){var DataRst={iRst:-4,iNewId:0};aResponse?aResponse(-4):0;return;}
				else if(data.length>0&&aGuestInfo.iGuestNo==0)
				{
					aGuestInfo.iGuestNo=data[0].NUMMER;
				}
				if(aGuestInfo.iGuestNo==0)
				{
					var InsertCol=["HOTELCODE","NAME","SEX","VIP","EMAIL","TELEX","NATION","ORT","SPRACHE","PASSPORT"];
					var InsertVal=["?","?","?","?","?","?","?","?","?","?"];
					var InPara=[aCode,aGuestInfo.iGuestName,aGuestInfo.iSex,aGuestInfo.iVip,aGuestInfo.iEmail,aGuestInfo.iMobile
					,aGuestInfo.iNation,aGuestInfo.iCity,aGuestInfo.iLang,aGuestInfo.iCardNo];
					MySql.InsertTableRow(DataTable.TableGuestInfo,InsertCol,InsertVal,InPara,
						function(Rst){
							if(Rst<0){
								var DataRst={iRst:Rst,iNewId:0};
								aResponse?aResponse(DataRst):0;
								return;
							}
							else{
								var DataRst={iRst:0,iNewId:Rst};
								aResponse?aResponse(DataRst):0;
								return;
							}
						});
				}else{
					var UpdateCol=["SEX","VIP","EMAIL","TELEX","NATION","ORT","SPRACHE"];
					var UpdateVal=["?","?","?","?","?","?","?"];
					var UpdatePara=[aGuestInfo.iSex,aGuestInfo.iVip,aGuestInfo.iEmail,aGuestInfo.iMobile
					,aGuestInfo.iNation,aGuestInfo.iCity,aGuestInfo.iLang];
					MySql.UpdateTable(DataTable.TableGuestInfo,UpdateCol,UpdateVal,"NUMMER="+aGuestInfo.iGuestNo,UpdatePara,
						function(Rst)
						{
							if(Rst<0){
								var DataRst={iRst:Rst,iNewId:0};
								aResponse?aResponse(DataRst):0;
								return;
							}
							else{
								var DataRst={iRst:0,iNewId:aGuestInfo.iGuestNo};
								aResponse?aResponse(DataRst):0;
								return;
							}
						});
				}
			});
	},
	//新增入住记录
	NewComeInGuest:function(MySql,aCode,aGuestArr,aResponse){
		if(aGuestArr===null||aGuestArr.length<1) 
		{aResponse?aResponse(-1):0;return;}
		var InsertCmd=["INSERT INTO ",DataTable.TableGuestCheckIn," (HOTELCODE,GASTNR,GASTNAME,ANREISE,ABREISE,ROOM_ID,ZIMMER,CHECK_FLAG,RT7,TEAM) VALUES ? "];
		var InsertPara=[aGuestArr.length];
		for( var i = 0; i < aGuestArr.length; i++){
			InsertPara[i]=[aCode,aGuestArr[i].iGuestNo,aGuestArr[i].iGuestName
			,JsOper.ToLongDateStr(new Date(aGuestArr[i].iCheckInTime*1000))
			,JsOper.ToLongDateStr(new Date(aGuestArr[i].iCheckOutTime*1000))
			,aGuestArr[i].iRoomId,aGuestArr[i].iRoomName
			,DataTable.FlagEmpty,aGuestArr[i].iCompany,aGuestArr[i].iTeamName];
		}
		MySql.BulkInsert(InsertCmd.join(''),InsertPara,function(Rst)
		{
			if(Rst===false)Rst=-1;
			aResponse?aResponse(Rst):0;	
		});
	},
	//更新入住
	Checkin:function(MySql,aRoomId,aResponse) {
	    var Today = JsOper.GetDateStr(0,0); //今天
		var Tomorrow=JsOper.GetDateStr(0,1); //明天
		var QPara=[aRoomId,Tomorrow,Today,DataTable.FlagEmpty];
		MySql.SelectTable(DataTable.TableGuestCheckIn,"1"," ROOM_ID=?  AND ANREISE <? AND ABREISE>?  AND CHECK_FLAG=? ",QPara
		  	,function(data){
		  		if(data===false){aResponse?aResponse(-1):0;return;}
		  		if(data.length===0){aResponse?aResponse(-4):0;return;}
				var updateCol=['CHECK_FLAG','ANREISE'];
				var updateVal=['?','NOW()'];
				var UPara=[DataTable.FlagCheckIn,aRoomId,Tomorrow,Today,DataTable.FlagEmpty];
				MySql.UpdateTable(DataTable.TableGuestCheckIn,updateCol,updateVal," ROOM_ID=?  AND ANREISE <? AND ABREISE>?  AND CHECK_FLAG=? ",UPara,
					function(Rst)
					{
						if(Rst===false)Rst=-1;
					  	aResponse?aResponse(Rst):0;	
					});
			});
	},
	//更新退房
	Checkout:function(MySql,aRoomId,aResponse) {
	    var updateCol=['CHECK_FLAG','ABREISE'];
		var updateVal=['?','NOW()'];
		var UPara=[DataTable.FlagCheckOut];
		MySql.UpdateTable(DataTable.TableGuestCheckIn,updateCol,updateVal,"ROOM_ID="+aRoomId,UPara,
			function(Rst)
			{
				if(Rst===false)Rst=-1;
			  	aResponse?aResponse(Rst):0;	
			});
	},
	//延长住房
	OverStay:function(MySql,aRoomId,aStayTime,aResponse){
		var StayDate=new Date(aStayTime*1000);
		var updateCol=['ABREISE'];
		var updateVal=['?'];
		var UPara=[JsOper.ToLongDateStr(StayDate),DataTable.FlagCheckIn];
		MySql.UpdateTable(DataTable.TableGuestCheckIn,updateCol,updateVal," CHECK_FLAG=? AND ROOM_ID="+aRoomId,UPara,
			function(Rst)
			{
				if(Rst===false)Rst=-1;
			  	aResponse?aResponse(Rst):0;	
			});
	},
	//取消预订
	CancelBooking:function(MySql,aRoomId,aGuestArr,aResponse) {
	  	if(aGuestArr.length===0)aResponse?aResponse(-4):0;
	  	var WhereSql=[' ROOM_ID='+aRoomId];
	  	WhereSql.push(" AND CHECK_FLAG='' AND ( ")
	  	for(var i in aGuestArr)
	  	{
	  		if(i>0)WhereSql(' OR ')
	  		WhereSql.push(' ( GASTNR='+aGuestArr[i].k+' AND UNIX_TIMESTAMP(ANREISE)='+aGuestArr[i].v+' )' );
	  	}	
	  	WhereSql.push(" ) ")
	  	console.log(WhereSql.join(''));
	  	MySql.DeleteTable(DataTable.TableGuestCheckIn,WhereSql.join(''),false,
	  		function(Rst){
				if(Rst===false)Rst=-1;
			  	aResponse?aResponse(Rst):0;	
	  		});
	},

}

exports.GuestModel=GuestModel;